package com.client;

import com.model.*;

import java.io.File;
import java.io.IOException;

/**
 * 考题管理模块
 */
public class ExamSystemView {
    private ClientInitClose clientInitClose;
    private ExamMessage examMessage;

    public ExamSystemView(ClientInitClose clientInitClose) {
        this.clientInitClose = clientInitClose;
    }

    /**
     * 考题管理界面
     */
    public void examSystemManageMenu() throws IOException, ClassNotFoundException {
        while (true){
            System.out.println("          考题管理系统          ");
            System.out.println("------------------------------");
            System.out.println("[1] 增加考题         [2] 删除考题");
            System.out.println("[3] 修改考题         [4] 查找考题");
            System.out.println("[5] 导入考题         [0] 退出   ");
            System.out.println("------------------------------");
            System.out.println("请输入您要操作的选项：");
            int option = ClientScanner.getScanner().nextInt();
            switch (option){
                case 0:
                    System.out.println("正在退出考题管理系统。。。。。。");
                    // 发送给服务器
                    clientInitClose.getObjectOutputStream().writeObject(new ExamMessage("quit", (Exam) null));
                    clientInitClose.getObjectOutputStream().flush();
                    return;
                case 1:
                    //System.out.println("增加考题");
                    // 客户端定义添加考题
                    System.out.println("请输入您要增加考题的id：");
                    int add_id = ClientScanner.getScanner().nextInt();
                    System.out.println("请输入您要增加考题的问题：");
                    String add_question = ClientScanner.getScanner().next();
                    System.out.println("请输入您要增加考题的选项：");
                    String add_answer = ClientScanner.getScanner().next();
                    System.out.println("请输入您要增加考题的正确答案：");
                    String add_rightAnswer = ClientScanner.getScanner().next();
                    Exam add_exam = new Exam(add_id,add_question,add_answer,add_rightAnswer);
                    // 发送给服务端
                    clientInitClose.getObjectOutputStream().writeObject(new ExamMessage("add",add_exam));
                    clientInitClose.getObjectOutputStream().flush();
                    // 接收服务器端
                    examMessage= (ExamMessage) clientInitClose.getObjectInputStream().readObject();

                    if (examMessage.getMessage().equals("success")){
                        System.out.println("当前考题添加成功！");
                    }else
                        System.out.println("当前考题系统中已有相同题号考题，无需再次添加！");
                    break;
                case 2:
                    //System.out.println("删除考题");
                    // 客户端定义删除考题
                    System.out.println("请输入您要删除考题的id：");
                    int delete_id = ClientScanner.getScanner().nextInt();
                    Exam delete_exam = new Exam(delete_id,"","","");
                    // 发送给服务端
                    clientInitClose.getObjectOutputStream().writeObject(new ExamMessage("delete",delete_exam));
                    clientInitClose.getObjectOutputStream().flush();
                    // 接收服务器端
                    examMessage = (ExamMessage) clientInitClose.getObjectInputStream().readObject();
                    if (examMessage.getMessage().equals("success")){
                        System.out.println("删除考题成功！");
                    }else
                        System.out.println("输入考题id有误，系统中无该id考题！");
                    break;
                case 3:
                    //System.out.println("修改考题");
                    // 客户端定义修改考题内容
                    System.out.println("请输入您要修改考题的id：");
                    int modify_id = ClientScanner.getScanner().nextInt();
                    System.out.println("请输入您要修改考题的题干：");
                    String modify_question = ClientScanner.getScanner().next();
                    System.out.println("请输入您要修改考题的选项：");
                    String modify_answer = ClientScanner.getScanner().next();
                    System.out.println("请输入您要修改考题的正确答案：");
                    String modify_rigthtAnswer = ClientScanner.getScanner().next();
                    Exam modify_exam = new Exam(modify_id,modify_question,modify_answer,modify_rigthtAnswer);
                    // 发送给服务端
                    clientInitClose.getObjectOutputStream().writeObject(new ExamMessage("modify",modify_exam));
                    clientInitClose.getObjectOutputStream().flush();
                    // 接收服务器端
                    examMessage = (ExamMessage) clientInitClose.getObjectInputStream().readObject();
                    if (examMessage.getMessage().equals("success")){
                        System.out.println("修改考题信息成功！");
                    }else
                        System.out.println("输入考题id有误，系统中无该id考题！");
                    break;
                case 4:
                    //System.out.println("查找考题");
                    // 客户端定义查找考题
                    System.out.println("请输入您要查找考题的题干内容：");
                    String find_question = ClientScanner.getScanner().next();
                    Exam find_exam = new Exam(0,find_question,"","");
                    // 发送给服务端
                    clientInitClose.getObjectOutputStream().writeObject(new ExamMessage("find",find_exam));
                    clientInitClose.getObjectOutputStream().flush();
                    // 接收服务器端
                    examMessage = (ExamMessage) clientInitClose.getObjectInputStream().readObject();

                    if (examMessage.getMessage().equals("success")){
                        System.out.println("查找的考题信息如下：");
                        System.out.println(examMessage.getExam().toString());
                    } else
                        System.out.println("系统中无该考题！");
                    break;
                case 5:
                    //System.out.println("导入考题");
                    // 客户端定义要导入考题
                    System.out.println("请输入您要导入考题的路径：");
                    String import_exam = ClientScanner.getScanner().next();
                    // 发送给客户端
                    clientInitClose.getObjectOutputStream().writeObject(new ExamMessage("import",new File(import_exam)));
                    clientInitClose.getObjectOutputStream().flush();
                    // 接收服务器端
                    examMessage = (ExamMessage) clientInitClose.getObjectInputStream().readObject();

                    if (examMessage.getMessage().equals("success")){
                        System.out.println("导入考题成功！");
                    }
                    else
                        System.out.println("导入考题失败！");
                    break;
                default:
                    System.out.println("您的输入有误，请重新输入！");
            }
        }
    }
}
